package com.haozhen.streamtransformation;

import com.haozhen.datasource.SelfRichParallel;
import com.haozhen.datasource.SelfSourceParallel;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

/**
 * @author haozhen
 * @email haozh@ync1.com
 * @date 2021/2/24  2:14
 */
public class ConnectDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> paraData = env.addSource(new SelfSourceParallel());

        DataStreamSource<String> richData = env.addSource(new SelfRichParallel());

        ConnectedStreams<String, String> connect = paraData.connect(richData);

        SingleOutputStreamOperator<Tuple2<String, String>> map = connect.map(new CoMapFunction<String, String, Tuple2<String, String>>() {
            @Override
            public Tuple2<String, String> map1(String value) throws Exception {
                return new Tuple2<>("para", value);
            }

            @Override
            public Tuple2<String, String> map2(String value) throws Exception {
                return new Tuple2<>("rich", value);
            }
        });

        map.print();

        env.execute();

    }
}
